﻿@UI.UICallOnly('')
export default class UIDefault extends UI.UIBehavior {
	// Character: Gameplay.Character;
	/* 解析资源ID列表 */
    private resolveString(assetIds: string): string[] {
        let assetIdArray: string[] = new Array<string>();
        let assetId: string = "";
        let s = assetIds.split("");
        for (let a of s) {
            if (a == ",") {
                assetIdArray.push(assetId);
                assetId = "";
            } else {
                assetId += a;
            }
        }
        if (assetId) {
            assetIdArray.push(assetId);
        }
        return assetIdArray;
    }

	/* 初始化资源 */
	private initAssets(assetIds: string): void {
		let assetIdArray = this.resolveString(assetIds);
		for (let element of assetIdArray) {
			Util.AssetUtil.asyncDownloadAsset(element)
		}
	}

	private character: Gameplay.Character


	/** 仅在游戏时间对非模板实例调用一次 */
    protected async onStart() {
		//初始化动画资源 
		this.initAssets("95777,61245")
		//设置能否每帧触发onUpdate
		this.canUpdate = false;

		this.character = (await Gameplay.asyncGetCurrentPlayer()).character
		
		//找到对应的跳跃按钮
        const JumpBtn = this.uiWidgetBase.findChildByPath('RootCanvas/Button_Jump') as UI.Button
		const AttackBtn = this.uiWidgetBase.findChildByPath('RootCanvas/Button_Attack') as UI.Button
		const InteractBtn = this.uiWidgetBase.findChildByPath('RootCanvas/Button_Interact') as UI.Button
		
		//点击跳跃按钮,异步获取人物后执行跳跃
        JumpBtn.onPressed.add(()=>{
			this.character.jump();
		})	

		//点击攻击按钮,异步获取人物后执行攻击动作
        AttackBtn.onPressed.add(()=>{
			//让动画只在上半身播放
			let anim1 = this.character.loadAnimation("61245");
			anim1.slot = Gameplay.AnimSlot.Upper;
			//角色执行攻击动作
			if(anim1.isPlaying){
				return
			}else{
				anim1.play();
			}
		})

		//点击交互按钮,异步获取人物后执行交互动作
        InteractBtn.onPressed.add(()=>{
			//让动画只在上半身播放
			let anim2 = this.character.loadAnimation("95777");
			anim2.slot = Gameplay.AnimSlot.Upper;
			//角色执行交互动作
			if(anim2.isPlaying){
				return
			}else{
				anim2.play();
			}
		})
    }
}
